package com.zhiyou100.blog.listener; /**
 * @author shkstart
 * @create 2021-10-26 8:47
 */

import com.zhiyou100.blog.entity.User;
import org.junit.Test;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

@WebListener("/*")
public class OnLinePeopleListener implements ServletContextListener,
        HttpSessionListener, HttpSessionAttributeListener {
    // 用户存储在线人数
    private static HashSet<Object> applicationSet = new HashSet<>();

    // Public constructor is required by servlet spec
    public OnLinePeopleListener() {
    }

    // -------------------------------------------------------
    // ServletContextListener implementation
    // -------------------------------------------------------
    public void contextInitialized(ServletContextEvent sce) {
      /* This method is called when the servlet context is
         initialized(when the Web application is deployed).
         You can initialize servlet context related data here.
      */
        //System.out.println("contextInitialized 被执行了");


    }


    public void contextDestroyed(ServletContextEvent sce) {
      /* This method is invoked when the Servlet Context 
         (the Web application) is undeployed or 
         Application Server shuts down.
      */
    }

    // -------------------------------------------------------
    // HttpSessionListener implementation
    // -------------------------------------------------------
    public void sessionCreated(HttpSessionEvent se) {
        /* Session is created. */
    }

    public void sessionDestroyed(HttpSessionEvent se) {
        /* Session is destroyed. */
    }

    // -------------------------------------------------------
    // HttpSessionAttributeListener implementation
    // -------------------------------------------------------

    public void attributeAdded(HttpSessionBindingEvent sbe) {

      /* This method is called when an attribute 
         is added to a session.
      */
        String name = sbe.getName();
        System.out.println("sessino 被创建 attributeAdded" + name);
        if (sbe.getName().contains("loginUser")) {
            HttpSession session = sbe.getSession();
            User user = (User) session.getAttribute("loginUser");
            if (user != null) {
                applicationSet.add(user.getUname());
                System.out.println("在线用户 loginUser = " + user + "," + applicationSet.size());
                sbe.getSession().setAttribute("applicationSet", applicationSet);
            }
        }


    }

    public void attributeRemoved(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute
         is removed from a session.
      */
        String name = sbe.getName();
        System.out.println("session 被删除 attributeRemoved" + name);

        /*if (sbe.getName().contains("loginUser")) {
            HttpSession session = sbe.getSession();
            User logoutUser = (User) session.getAttribute("loginUser");
            System.out.println("logoutUser = " + logoutUser);
            applicationSet.remove(logoutUser);
            System.out.println("用户退出 logoutUser" + logoutUser + "," + applicationSet.size());
            session.setAttribute("applicationSet", applicationSet);
        }*/

    }

    public void attributeReplaced(HttpSessionBindingEvent sbe) {
      /* This method is invoked when an attribute
         is replaced in a session.
      */
    }
}
